package com.mdd.front.service.account.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.account.AccountLog;
import com.mdd.common.mapper.account.AccountLogMapper;
import com.mdd.common.utils.TimeUtil;
import com.mdd.front.LikeFrontThreadLocal;
import com.mdd.front.service.account.IAccountLogService;
import com.mdd.front.validate.PageParam;
import com.mdd.front.vo.account.AccountLogListVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@Service
public class AccountLogServiceImpl implements IAccountLogService {

    @Resource
    private AccountLogMapper accountLogMapper;
    @Override
    public PageResult<AccountLogListVo> list(PageParam pageParam, Map<String, String> params) {
        Integer pageNo = pageParam.getPageNo();
        Integer pageSize = pageParam.getPageSize();
        QueryWrapper<AccountLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", LikeFrontThreadLocal.getUserId());
        queryWrapper.orderByDesc("create_time");

        accountLogMapper.setSearch(queryWrapper, params, new String[]{
                "=:action@action:int",
        });
        IPage<AccountLog> iPage = accountLogMapper.selectPage(new Page<>(pageNo, pageSize), queryWrapper);
        LinkedList<AccountLogListVo> list = new LinkedList<>();
        for (AccountLog accountLog : iPage.getRecords()) {
            AccountLogListVo vo = new AccountLogListVo();
            BeanUtils.copyProperties(accountLog, vo);
            vo.setActionDesc(accountLog.getAction());
            vo.setChangeTypeDesc(accountLog.getChangeType());
            vo.setCreateTime(TimeUtil.timestampToDate(accountLog.getCreateTime()));
            list.add(vo);
        }
        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }
}
